package cn.tt.tuantuanerp.tuantuanerp.product.webapi.controller;


import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.productModule.SkuAddNewBasicDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.productModule.SkuAddNewDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.productModule.SkuUpdateDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.entity.Sku;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.*;
import cn.tt.tuantuanerp.tuantuanerp.comment.security.LoginPrincipal;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.JsonResult;
import cn.tt.tuantuanerp.tuantuanerp.product.service.ISkuService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 商品的controller层
 *
 * @author 大大大威
 * @version 0.0.1
 */
@RestController
@RequestMapping("/skus")
@Slf4j
@Api(tags = "2. 商品管理模块")
public class SkuController {

    @Autowired
    ISkuService skuService;

    /**
     * 添加新sku
     *
     * @param skuAddNewDTO 添加新sku 的数据封装,
     * @return 返回添加结果
     */
    @ApiOperation("新增SKU(全部信息)")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(SkuAddNewDTO skuAddNewDTO,
                             @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("接收到添加新sku请求,参数:{}",skuAddNewDTO);
        skuService.addNew(skuAddNewDTO, loginPrincipal);

        return JsonResult.ok();
    }

    /**
     * 添加新sku
     *
     * @param skuAddNewBasicDTO 添加sku的基础数据
     * @return 返回添加结果
     */
    @ApiOperation("新增SKU(基础信息)")
    @ApiOperationSupport(order = 101)
    @PostMapping("/add-new/basic")
    public JsonResult<Void> addNew(SkuAddNewBasicDTO skuAddNewBasicDTO
            , @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("接收到添加新sku请求,参数:{}",skuAddNewBasicDTO);
        skuService.addNew(skuAddNewBasicDTO, loginPrincipal);
        return JsonResult.ok();
    }

    /**
     * 删除sku
     * @param id skuId
     * @return 返回删除结果
     */
    @ApiOperation("删除SKU")
    @ApiOperationSupport(order = 202)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id
            , @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("接收到删除sku请求,参数id:{}",id);
        skuService.deleteById(id, loginPrincipal);
        return JsonResult.ok();
    }

    /**
     * 根据skuId修改
     *
     * @param skuUpdateDTO 需要修改的sku数据
     * @return 返回修格结果
     */
    @ApiOperation("修改SKU")
    @ApiOperationSupport(order = 303)
    @PostMapping("/update")
    public JsonResult<Void> updateById(SkuUpdateDTO skuUpdateDTO
            , @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("接收到修改sku请求,参数:{}",skuUpdateDTO);
        skuService.updateById(skuUpdateDTO, loginPrincipal);
        return JsonResult.ok();
    }

    /**
     * 分页查询
     * @return 返回查询结果
     */
    @ApiOperation("查询sku列表")
    @ApiOperationSupport(order = 403)
    @GetMapping("/{pageIndex:[0-9]+}/{pageSize:[0-9]+}")
    public JsonResult<List<SkuListItemVO>> list(@PathVariable int pageIndex, @PathVariable int pageSize, @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("接收到查询sku列表请求");
        System.out.println("pageIndex = " + pageIndex + ", pageSize = " + pageSize + ", loginPrincipal = " + loginPrincipal);
        return JsonResult.ok(skuService.list(pageIndex,pageSize,loginPrincipal));
    }

    /**
     * 根据skuId查询
     * @return 返回查询结果
     */
    @ApiOperation("查询sku")
    @ApiOperationSupport(order = 407)
    @GetMapping("/{skuId:[0-9]+}/list-item")
    public JsonResult<SkuListItemVO> getListItemById(@PathVariable Long skuId) {
        log.debug("接收到查询单个sku列表请求");
        return JsonResult.ok(skuService.getListItemById(skuId));
    }

    /**
     * 查询Sku数量
     * @return 返回查询结果
     */
    @ApiOperation("查询sku数量")
    @ApiOperationSupport(order = 403)
    @GetMapping("/count")
    public JsonResult<Integer> count( @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("接收到查询sku数量请求");
        int count = skuService.count();
        log.debug("查到{}个sku",count);
        return JsonResult.ok(count);
    }

    /**
     * 查询sku详情
     * @return 返回查询结果
     */
    @ApiOperation("查询sku详情")
    @ApiOperationSupport(order = 404)
    @GetMapping("/{id:[0-9]+}/detail")
    public JsonResult<SkuDetailsVO> getDetailById(@PathVariable Long id,
                                                  @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("接收到查询sku详情请求,查询skuId:{}",id);

        return JsonResult.ok(skuService.getDetailById(id, loginPrincipal));
    }

    /**
     * 为添加订单查询sku
     * @return 返回查询结果
     */
    @ApiOperation("为添加订单查询sku")
    @ApiOperationSupport(order = 405)
    @GetMapping("/{id:[0-9]+}/sku-for-order")
    public JsonResult<SkuForOrderVO> getSkuForOrder(@PathVariable Long id,
                                                    @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("接收到为添加订单查询sku请求,查询skuId:{}",id);
        return JsonResult.ok(skuService.getSkuForOrder(id, loginPrincipal));
    }

    /**
     * 为添加sku查询sku
     * @return 返回查询结果
     */
    @ApiOperation("为添加sku查询sku")
    @ApiOperationSupport(order = 408)
    @GetMapping("/{id:[0-9]+}/for-add-new")
    public JsonResult<Sku> getSkuForAddNew(@PathVariable Long id) {
        log.debug("接收到为添加订单查询sku请求,查询skuId:{}",id);
        return JsonResult.ok(skuService.getSkuInfoByApiForAddNew(id));
    }

    /**
     * 为添加盘点批次查询sku
     * @return 返回查询结果
     */
    @ApiOperation("为添加盘点批次查询sku")
    @ApiOperationSupport(order = 405)
    @GetMapping("/{id:[0-9]+}/sku-for-check")
    public JsonResult<SkuForCheckVO> getSkuForCheck(@PathVariable Long id,
                                                    @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("接收到为添加盘点批次查询sku请求,查询skuId:{}",id);
        return JsonResult.ok(skuService.getSkuFroCheck(id, loginPrincipal));
    }

    /**
     * 为添加采购单查询sku
     * @return 返回查询结果
     */
    @ApiOperation("为添加采购单查询sku")
    @ApiOperationSupport(order = 405)
    @GetMapping("/{id:[0-9]+}/sku-for-purchase")
    public JsonResult<SkuForPurchaseVO> getSkuForPurchase(@PathVariable Long id,
                                                          @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("接收到为添加采购单查询sku请求,查询skuId:{}",id);
        return JsonResult.ok(skuService.getSkuFroPurchase(id, loginPrincipal));
    }

    /**
     * 查询缺货sku
     * @return 返回查询结果
     */
    @ApiOperation("查询缺货sku")
    @ApiOperationSupport(order = 406)
    @GetMapping("/stock-out")
    public JsonResult<List<SkuForPurchaseVO>> listStockOut(int pageIndex,int pageSize) {
        log.debug("接收到为添加采购单查询缺货sku请求");
        return JsonResult.ok(skuService.listStockOut(pageIndex,pageSize));
    }

    /**
     * 查询所有sku
     */
    @GetMapping("/getList")
    @ApiOperation ( "查询列表" )
    public  JsonResult<List<SkuListItemVO>> getList(){
        return JsonResult.ok (skuService.list ());
    }

}
